/****************************************************************************
    Kantaoke, a karaoke game made with BennuGD.
    Copyright (C) 2010 Joseba García Echebarria

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#ifndef __MAIN_PRG__
#define __MAIN_PRG__

// Includes preprocessor definitions needed below
include "inc/declares.inc";

// Modules shipped with BennuGD
import "mod_say";
import "mod_key";
import "mod_video";
import "mod_text";
import "mod_proc";
import "mod_file";
import "mod_string";
import "mod_map";
import "mod_timers";
import "mod_dir";
import "mod_wm";
import "mod_draw";
import "mod_screen";
import "mod_regex";
import "mod_sys";
import "mod_rand";
import "mod_time";
import "mod_mouse";
import "mod_sys";
import "mod_math";

// Custom modules
// IMAGE: Highly appreciated
import "image";
// ICONV: Really needed
import "mod_iconv";
// FMOD: Useful for media playback and sound input
#ifndef NOFMOD
import "mod_fmodex";
#else
import "mod_sound";		// Fallback
#endif
// PANGO: Appreciated
#ifndef CRAPPY_FONTS
import "pango";
#endif
// VLC: Needed for video
#ifndef NO_VLC
import "mod_vlc";
#endif

// This is the main process for the game. It basically sets the resolution
// for you, finds the songs in your songs dir and then tells the menu to appear.
Process main()
Private
  int aux_x=0, aux_y=0;
  int gr_loading=0, gr_bennu_logo=0, gr_fmod_logo=0,
      gr_made_by_me=0;

Begin
  datadir = cd()+"/";
  developerpath = developerpath+"/";
  if(OS_ID == OS_WIN32)
    custompath = "APPDATA";
  else
    developerpath = "."+developerpath;
  end;
    
  //Start Bennu graphics, set some window vars
  set_fps(MAIN_FPS, 0);
  alpha_steps = 30;
  set_title(gamename);
  rand_seed(time());
  get_desktop_size(&aux_x, &aux_y);
  resx = (float)aux_x; resy = (float)aux_y;
  dbg_print("Trying video mode: "+(int)resx+"x"+(int)resy+"x"+BPP);
  // I cannot set the screen to the desktop resolution
  if(! mode_is_ok(resx, resy, BPP, MODE_FULLSCREEN))
    dbg_print("FATAL ERROR: Sorry, I couldn't set the desktop resolution to "+
        resx+"x"+resy);
    exit();
  end;

  #ifdef DEBUG
  set_mode(resx, resy, BPP, MODE_WINDOW);
  #else
  set_mode(resx, resy, BPP, MODE_FULLSCREEN);
  #endif

  #ifdef CRAPPY_FONTS
  font = load_fnt(datadir+"fnt/font_white.fnt");
  #endif

  // Display a "Loading message" and the Bennu logo
  gr_loading = pango_render(OPEN_HIGHLIGHT+'Loading...'+CLOSE_HIGHLIGHT);
  gr_made_by_me = pango_render(OPEN_NOHIGHLIGHT_SMALL+'Proudly made by Joseba Garcia Etxebarria using BennuGD and FMOD'+CLOSE_NOHIGHLIGHT_SMALL);
  gr_bennu_logo = load_image(datadir+"/img/bennugd-logo.jpg");
  gr_fmod_logo  = load_image(datadir+"/img/fmod-logo.jpg");
  
  // Blit the three pre-rendered graphics on the screen
  aux_y = 5*resy/6;
  put(0, gr_bennu_logo, resx/2-graphic_info(0, gr_bennu_logo, G_WIDTH)/1.5, aux_y);
  put(0, gr_fmod_logo, resx/2+graphic_info(0, gr_fmod_logo, G_WIDTH)/1.5, aux_y);
  aux_y -= graphic_info(0, gr_bennu_logo, G_HEIGHT)/1.3;
  put(0, gr_made_by_me, resx/2, aux_Y);
  aux_y -= 5*graphic_info(0, gr_made_by_me, G_HEIGHT);
  put(0, gr_loading, resx/2, aux_y);
  
  // Unload the graphics, as they're not needed anymore
  unload_map(0, gr_bennu_logo);
  unload_map(0, gr_fmod_logo);
  unload_map(0, gr_made_by_me);
  unload_map(0, gr_loading);

  // For some reason, I need to frame twice before the blitted screen appears...
  FRAME; FRAME;
  
  // Look for songs in the songs dir
  find_songs();
  
  // No songs found? Display an error, then quit.
  if (songs_found == 0)
    no_songs_menu();
    return 0;
  end;
  
  // Start pre-loading the covers, and wait for most of them to load
  pre_render_covers();
  
  // Clear the logos from the screen
  fade(0, 0, 0, 15);
  while(fading)
    FRAME;
  end;

  clear_screen();
  
  fade(100, 100, 100, 15);
  while(fading)
    FRAME;
  end;

  //We've got at least one song, show the normal menu
  //mouse_controller();       // The mouse is not really needed in the menu
  menu();
End;

include "inc/charset.inc";
include "inc/misc.inc";
include "inc/lyrics.inc";
include "inc/playback.inc";
include "inc/menu.inc";
#endif
